<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm" v-show="showSearch">
      <el-form-item label="计息开始" prop="rateDateStart">
        <el-date-picker clearable placeholder="选择计息开始日期"
                        size="small"
                        type="date"
                        v-model="queryParams.rateDateStart"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计息结束" prop="rateDateEnd">
        <el-date-picker clearable placeholder="选择计息结束日期"
                        size="small"
                        type="date"
                        v-model="queryParams.rateDateEnd"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          @click="handleAdd"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
        >分配利息
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          @click="handleDelete"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="interestShareList" @selection-change="handleSelectionChange" v-loading="loading">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column :formatter="numberFormat" align="center" label="应分利息" prop="shouldMoney" width="150"/>
      <el-table-column :formatter="numberFormat" align="center" label="实分利息" prop="rateMoney" width="150"/>
      <el-table-column :formatter="numberFormat" align="center" label="利息差额" prop="subMoney" width="150"/>
      <el-table-column align="center" label="结息日" prop="rateDate" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="缴费截至日期" prop="costDate" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.costDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" prop="remark" width="300"/>
      <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleSelect(scope.row)"
            icon="el-icon-search"
            size="mini"
            type="text"
            :style="{ display: scope.row.rateDate==null?'none':'' }"
          >明细
          </el-button>

          <el-button
            @click="remarkUpdate(scope.row)"
            icon="el-icon-edit"
            size="mini"
            type="text"
            :style="{ display: scope.row.rateDate==null?'none':'' }"
          >修改备注
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
      v-show="total>0"
    />

    <!-- 添加或修改利息分摊对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form :model="form" :rules="rules" label-width="80px" ref="form">
        <el-form-item label="分摊金额" prop="rateMoney">
          <el-input :style="{width: '100%'}" clearable placeholder="请输入分摊金额" v-model="form.rateMoney"/>
        </el-form-item>
        <el-form-item label="计息日期" prop="rateDate">
          <el-date-picker :style="{width: '100%'}" clearable
                          placeholder="选择计息日期"
                          type="date"
                          v-model="form.rateDate"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="缴费日期" prop="costDate">
          <el-date-picker :style="{width: '100%'}" clearable
                          placeholder="选择缴费截至日期"
                          type="date"
                          v-model="form.costDate"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input placeholder="请输入内容" type="textarea" v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="submitForm" type="primary">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-form :inline="true" :model="queryParams1" label-width="68px" v-show="showSearch" ref="queryForm1" >

      <el-dialog :title="titleDetail" :visible.sync="openDetail" append-to-body width="1000px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input
            v-model="queryParams1.houseId"
            :style="{width: '200px'}"
            size="small" clearable
            placeholder="请输入房屋编号"
          />
          <el-button @click="handleQuery1" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
          <el-button @click="resetQuery1" icon="el-icon-refresh" size="mini">重置</el-button>
        </el-form-item>

        <el-table :data="interestShareDetailList" v-loading="loading">
          <el-table-column align="center" label="房屋编号" prop="houseId"/>
          <el-table-column align="center" label="实分利息" prop="rateMoney"/>
          <el-table-column align="center" label="结息日" prop="rateDate"/>
          <el-table-column align="center" label="计息方式" prop="rateType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.HOUSE004" :value="scope.row.rateType"/>
            </template>
          </el-table-column>
          <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button
                @click="rateUpdate(scope.row)"
                icon="el-icon-edit"
                size="mini"
                type="text"
              >修改利息
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :limit.sync="queryParams1.pageSize"
          :page.sync="queryParams1.pageNum"
          :total="totalDetail"
          @pagination="handleQuery1"
          v-show="totalDetail>0"
        />


      </el-dialog>
    </el-form>


    <el-dialog :title="titleRemark" :visible.sync="openRemark" append-to-body width="500px">
      <el-form :model="formRemark" :rules="rulesRemark" label-width="80px" ref="formRemark">
        <el-form-item label="备注" prop="remark">
          <el-input placeholder="请输入内容" type="textarea" v-model="formRemark.remark"/>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="submitRemarkForm" type="primary">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="titleRate" :visible.sync="openRate" append-to-body width="500px">
      <el-form :model="formRate" :rules="rulesRate" label-width="80px" ref="form">
        <el-form-item label="分摊金额" prop="rateMoney">
          <el-input :style="{width: '100%'}" clearable placeholder="请输入分摊金额" v-model="formRate.rateMoney"/>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="submitRateForm" type="primary">确 定</el-button>
        <el-button @click="cancelRate">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    addInterestShare,
    delInterestShare,
    getInterestShare,
    getRateDetail,
    listInterestShare,
    listRateDetail,
    updateInterestShare,
    updateRemarkT,
    updateRateDetailT
  } from "@/api/ibatis/interestShare";

  export default {
    name: "InterestShare",
    dicts: ['HOUSE004'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        totalDetail: 0,
        // 利息分摊表格数据
        interestShareList: [],
        interestShareDetailList: [],
        titleDetail: "",
        titleRemark: "",
        openDetail: false,
        openRemark: false,
        // 弹出层标题
        title: "",
        titleRate: "",
        // 是否显示弹出层
        open: false,
        openRate: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          rateDateStart: null,
          costDateEnd: null,
          rateDate: null,
          costDate: null,
        },
        queryParams1: {
          pageNum: 1,
          pageSize: 10,
          rateId: null,
          houseId: null,
        },
        // 表单参数
        form: {},
        formRemark: {},
        formRate: {},
        // 表单校验
        rules: {},
        rulesRemark: {},
        rulesRate: {
          rateMoney: [
            {required: true, message: "分摊金额不能为空", trigger: "blur"}
          ],
        },
      };
    },
    created() {
      this.getList();
    },
    methods: {
      numberFormat(row, column, cellValue) {
        cellValue += '';
        if (!cellValue.includes('.')) cellValue += '.';
        return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
          return $1 + ','
        }).replace(/\.$/, '')
      },
      /** 查询利息分摊列表 */
      getList() {
        this.loading = true;
        listInterestShare(this.queryParams).then(response => {
          this.interestShareList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.areaSelect();
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.openRemark = false;
        this.reset();
      },
      cancelRate() {
        this.openRate = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          rateId: null,
          shouldMoney: null,
          rateMoney: null,
          subMoney: null,
          rateDate: null,
          costDate: null,
          remark: null
        };
        this.formRemark = {};
        this.resetForm("form");
        this.resetForm("formRemark");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      resetQuery1() {
        this.queryParams1.houseId = null;
        this.handleQuery1();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.rateId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length
      },

      handleSelect(row) {
        this.openDetail = true;
        this.titleDetail = "查看分摊明细";
        this.resetForm("queryForm1");
        this.queryParams1.rateId = row.rateId;
        listRateDetail(this.queryParams1).then(response => {
          this.interestShareDetailList = response.rows;
          this.totalDetail = response.total;
          this.loading = false;
        });
      },

      handleQuery1(){
        listRateDetail(this.queryParams1).then(response => {
          this.interestShareDetailList = response.rows;
          this.totalDetail = response.total;
          this.loading = false;
        });
      },

      getRateDetail() {
        listRateDetail(this.queryParams1).then(response => {
          this.interestShareDetailList = response.rows;
          this.totalDetail = response.total;
          this.loading = false;
        });
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加利息分摊";
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.rateId != null) {
              updateInterestShare(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.open = false;
              addInterestShare(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const rateIds = row.rateId || this.ids;
        this.$modal.confirm('是否确认删除利息分摊编号为"' + rateIds + '"的数据项？').then(function () {
          return delInterestShare(rateIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('ibatis/interestShare/export', {
          ...this.queryParams
        }, `interestShare_${new Date().getTime()}.xlsx`)
      },

      remarkUpdate(row) {
        this.reset();
        const rateId = row.rateId;
        getInterestShare(rateId).then(response => {
          this.formRemark = response.data;
          this.openRemark = true;
          this.titleRemark = "修改备注";
        });
      },

      submitRemarkForm() {
        updateRemarkT(this.formRemark).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openRemark = false;
          this.getList();
        });
      },

      rateUpdate(row) {
        this.reset();
        const rateDetailId = row.rateDetailId;
        getRateDetail(rateDetailId).then(response => {
          this.formRate = response.data;
          this.openRate = true;
          this.titleRate = "修改分摊金额";
        });
      },



      submitRateForm() {
        updateRateDetailT(this.formRate).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openRate = false;
          this.getRateDetail();
        });
      },
    }
  };
</script>
